package com.heima.Control;


import com.heima.poji.Emp;
import com.heima.poji.Result;
import com.heima.service.LoginService;
import com.heima.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginContral {
    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result loginByName(@RequestBody Emp emp) {
        log.info("用户登录功能");
        Emp emp1 = loginService.loginByName(emp);
        if (emp1 != null) {

            Map<String, Object> stringObjectMap = new HashMap<>();
            stringObjectMap.put("id", emp.getId());
            stringObjectMap.put("name", emp.getName());
            String jwt = JwtUtil.generateJwt(stringObjectMap);
           return Result.success(jwt);
        } else {
            return Result.error("登录失败");
        }
    }
}
